package com.dengyuanke.admin.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.ServiceType;
import com.dengyuanke.service.IServiceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品类型id 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-07
 */
@Api(tags = "服务类型相关API")
@RestController
public class ServiceTypeController {

    @Autowired
    private IServiceTypeService serviceTypeService;

    /**
     * 获得所有的服务类型
     * @return
     */
    @ApiOperation(value="获取所有服务类型")
    @PostMapping("/getallservicetype")
    public JsonResult getAllServiceType(){
        List<ServiceType> serviceTypes = serviceTypeService.selectList(new EntityWrapper<ServiceType>().eq("is_del", 0));
        return JsonResult.renderSuccess(serviceTypes);
    }

    /**
     * 添加服务类型
     * @param serviceType
     * @return
     */
    @ApiOperation(value = "添加服务类型",notes = "传入ServiceType实体")
    public JsonResult addServiceType(ServiceType serviceType){
            serviceType.setId(IdWorker.get32UUID());
            serviceType.setCreateTime(new Date());
        boolean insert = serviceTypeService.insert(serviceType);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    /**
     * 更新服务类型
     * @param serviceType
     * @return
     */
    @ApiOperation(value = "更新服务类型",notes = "传入带Id的ServiceType实体")
    public JsonResult updateServiceType(ServiceType serviceType){
        if(StringUtils.isEmpty(serviceType.getId())){
            return JsonResult.renderFail("更新失败，未传入id");
        }
        serviceType.setUpdateTime(new Date());
        boolean insert = serviceTypeService.updateById(serviceType);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

}
